package com.study.testShiro.utils;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

/**
 * shiro工具类
 *
 */
public class EncryptUtil {
	
	/**
	 * 获取与shiro对应的密文
	 * @param password 需要加密的明文密码
	 * @param salt 加密盐
	 * @param hashIterations  hash次数
	 * @param encyMode 加密方式
	 * @return
	 */
	public static String getPass(String password, String salt, int hashIterations,String encyMode) {
		SimpleHash simpleHash = new SimpleHash(encyMode,password, ByteSource.Util.bytes(salt),hashIterations);
		return simpleHash.toHex();
	}
	
	/**
	 * 生成随机盐
	 * @return
	 */
	public static String RodomSalt() {
		// 一个Byte占两个字节，此处生成的3字节，字符串长度为6
        SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
        String hex = secureRandom.nextBytes(3).toHex();
        return hex;
	}

	public static void main(String[] args) {
		String password  = "123456";

        String salt = RodomSalt();
        
        int hashIterations = 2;

        SimpleHash simpleHash = new SimpleHash("md5",password, ByteSource.Util.bytes(salt),hashIterations);

        System.out.println(simpleHash.toHex());
	}

}
